#!/usr/bin/env python
# -*- coding: utf-8 -*- #   
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import cgi
from google.appengine.api import users
import Cookie,urllib
from django.utils import simplejson
from google.appengine.api import urlfetch

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp.util import run_wsgi_app
import time
from google.appengine.ext import db
import SiteType
class Search(webapp.RequestHandler):
	def post(self):
		user=SiteType.Header(self)
		self.response.out.write(u"""<div align="center">""")
		if user:
			if SiteType.ismember(user):
				name=self.request.get("name")
				school=self.request.get("school")
				worked=self.request.get("worked")
				place=self.request.get("place")
				major=self.request.get("major")
				year=self.request.get("year")
				if name=="" and school=="" and worked=="" and place=="" and major=="" and year=="":
					self.response.out.write(u"""请输入查询条件""")
					self.response.out.write(u"</div>")
					SiteType.Ender(self)
					return 
				searchstr=""
				ans=SiteType.Member.all()
				mems=[]
				if name!="" :
					for mem in ans:
						if mem.name_chs==name or mem.name_eng==name:
							mems.append(mem)
				else:
					mems=ans
				ans=mems
				mems=[]
				if school!="" :
					for mem in ans:
						if school in mem.universities:
							mems.append(mem)
				else:
					mems=ans
				ans=mems
				mems=[]
				if worked!="" :
					for mem in ans:
						if mem in mems:
							continue
						if mem.worked:
							for wk in mem.worked:
								if wk.find(worked)!=-1:
									mems.append(mem)
									
				else:
					mems=ans
				ans=mems
				
				mems=[]
				if place!="" :
					for mem in ans:
						if mem in mems:
							continue
						if mem.locations:
							for wk in mem.locations:
								if wk.find(place)!=-1:
									mems.append(mem)
									
				else:
					mems=ans
				ans=mems
				mems=[]
				if  major!="" :
					for mem in ans:
						if mem in mems:
							continue
						if mem.major:
							for wk in mem.major:
								if wk.find(u"%s"%major)!=-1:
									mems.append(mem)
									
				else:
					mems=ans
				ans=mems
				mems=[]
				if  year!="" :
					for mem in ans:
						if mem in mems:
							continue
						if int(year)==mem.year_of_grad:
							mems.append(mem)
									
				else:
					mems=ans
				ans=mems
				if len(ans)==0:
					self.response.out.write(u"""没有符合条件的查询""")
					self.response.out.write(u"</div>")
					SiteType.Ender(self)
					return 
				for mem in ans:
					self.response.out.write(u"""<br><h1>%s</h1>"""%mem.name_chs)
					self.response.out.write(u"<br>%s届"%mem.year_of_grad+u"%s<br>"%mem.class_in_school)
					self.response.out.write(u"联系方式:Email:%s"%mem.username.email()+u"<br>电话：%s"%mem.phone)		
					
			else:
				self.response.out.write(u"""只有校友会认证成员可以访问数据库""")
		else:
			self.response.out.write(u"""只有校友会认证成员可以访问数据库""")
		self.response.out.write(u"</div>")
		SiteType.Ender(self)
class database(webapp.RequestHandler):
	def get(self):
		user=SiteType.Header(self)
		self.response.out.write(u"""<div align="center">""")
		self.response.out.write(u"""<head>
<script type="text/javascript">
function validate_form(thisform)
{
with (thisform)
  {
  if(year.value=="")return ture;
  if (Number(year.value)>1900&&Number(year.value)<2100)
  {return true;}
  alert("请输入合理入学年份");
  return false;
  }
}
</script>
</head>""")
		if user:
			if SiteType.ismember(user):
				self.response.out.write(u"""<fieldset><legend>查询:</legend><body><form name="input" action="/search?" onsubmit="return validate_form(this);" method="post">""")
				self.response.out.write(u"""<br>　按姓名:<input type="text" name="name" />""")
				self.response.out.write(u"""<br>　按学校:<input type="text" name="school" />""")
				self.response.out.write(u"""<br>　按工作:<input type="text" name="worked" />""")
				self.response.out.write(u"""<br>　按地点:<input type="text" name="place" />""")
				self.response.out.write(u"""<br>　按专业:<input type="text" name="major" />""")
				self.response.out.write(u"""<br>按毕业年:<input type="text" name="year" /><br><input type="submit" value="查询" />""")
				self.response.out.write(u"</body></fieldset>")
			else:
				self.response.out.write(u"""只有校友会认证成员可以访问数据库""")
		else:
			self.response.out.write(u"""只有校友会认证成员可以访问数据库""")
		self.response.out.write(u"</div>")
		SiteType.Ender(self)
		
		

def main():
  application = webapp.WSGIApplication([('/database', database),('/search.*',Search)],
                                       debug=True)
  util.run_wsgi_app(application)

if __name__ == '__main__':
  main()
